﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using MvcAppFinal1.Models.OMS.Domain.Model;
using MvcAppFinal1.Models.OMS.Infrastracture.Repositories;

namespace MvcAppFinal1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            User user1 = new User { UserId = 1, LoginName = "Login" };
            User user2 = new User { UserId = 2, LoginName = "Login" };
            ViewBag.UserId = user1.UserId;
            ViewBag.UserLogin = user1.LoginName;
            
            UserRepository userRep = new UserRepository();
            // Add user
            var addUser = userRep.Add(user1);
            userRep.SaveChanges();
            if (addUser != null)
            {
                ViewBag.Add = addUser.UserId + " user not add with id";
            }
            else ViewBag.Add = "Add 1 user";

            // Delete user
            var delUser = userRep.Delete(user1);
            userRep.SaveChanges();
            if (delUser != null)
            {
                ViewBag.Del = " user not delete with id " + delUser.UserId;
            }
            else ViewBag.del = "delete 1 user";

            // Delete all users with "Login"
            var userDel = userRep.UserToQuery().Select(user => user).Where(user => user.LoginName == "Login");
            foreach (var item in userDel)
            {
                userRep.Delete(item);
            }
            userRep.SaveChanges();

            // Get by Id
            var usById = userRep.GetById(2);
            // Count
            var usId = from user in userRep.Users select user;
            ViewBag.Count = usId.Count() + " Count";
            if (usById != null)
            {
                ViewBag.GetById = "Id is " + usById.UserId;
            }
            else ViewBag.GetById = "No Id";
            // Update user
            var updUser = new User { UserId = 41, LoginName = "LoginUpdate" };
            bool isUpdate = userRep.Update(updUser);
            ViewBag.IsUpdate = isUpdate;
            userRep.SaveChanges();

            return View(userRep.Users.ToList());
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
